<link href="Style.css" rel="stylesheet" type="text/css" />
<?php
session_start(); 

require("sql_functions.php");

if(isset($_SESSION['user']))
{

  $user = $_SESSION['user'];
	$defaultIDString = "(Save to assign)";
		
  // If you came to this page from editing and submitting the form on this page
	if(isset($_POST['formTitle']) && isset($_POST['formDesc']) && isset($_POST['projectID']))
	{
		$pTitle = $_POST['formTitle'];
		$pDesc = $_POST['formDesc'];
    $pid = $_POST['projectID'];
		$team = $_POST['isOnProject'];
    
    echo "$team";
 // Check that all inputs are entered
		if(empty($_POST['formTitle']) || empty($_POST['formDesc']))
		{	
			    $err = 'Empty fields are not allowed' ; 	
		}
		else
		{
    //Editing existing project
			if(strcmp($pid, $defaultIDString))
			{	
				$project = sql_editProject($pid, $user, $pTitle, $pDesc, $team);
			}

    //Creating a new project
			else
			{					
				$project = sql_createProject($user, $pTitle, $pDesc, $team);
			}
		}
		echo "$err";

    $pid = $project["ID"];
    
    header('Location: project_info.php?projectID=' . $pid);		
	}
	
  //If you came from a link to this page
	else if(isset($_GET['projectID'])) 
	{ 	
		$pid = $_GET['projectID'];


		$project = sql_getProject($pid);	
    

    if($project == -1)
    {
      header('Location: project_info.php');
    }
  
		$pTitle = $project["Title"];
		$pDesc = $project["Description"];
		$pManager = $project["ManagerEmail"];					
		$employee = sql_getEmployee($pManager);
		$firstName = $employee['FirstName'];
		$lastName = $employee['LastName'];
	}

  //If this is a new project
	else
	{
		$pid = $defaultIDString;
		$pTitle = "";
		$pDesc = "";
		$pManager = $user;
	}
	
	$possible = sql_getPossibleTeamMembers($pManager);
	
	
	echo "<html>"; 
	echo "<head>"; 
	echo "</head>"; 
	echo "<body>"; 
	echo "<p>Project Info"; 
	echo "<p>Project ID: $pid";
	echo "<form action=\"\" method=\"POST\">"; 
	echo "<p>Project Title:";
	echo "<input name=\"formTitle\" value=\"$pTitle\" />"; 
	echo "</p>"; 
	echo "<p>Description:";  
	echo "<input name=\"formDesc\" value=\"$pDesc\" />"; 
	echo "</p>"; 
	echo "<p>Project Manager: $firstName $lastName";
	echo "<br><br><p>Project Team";
	echo "<br><p>";
	
	echo "<table border='1'>";
	while($row = mysql_fetch_array($possible))
	{
		
		$email = $row['Email'];
		$firstName = $row['FirstName'];
		$lastName = $row['LastName'];

		$isOnProject = sql_isOnProject($email, $pid);

		echo "<tr><td>";
		echo "<input type=\"checkbox\" name=\"isOnProject[]\" value=\"$email\" ";
		if($isOnProject)
		{
			echo "checked=\"yes\" ";
		}
		echo " />";
		echo "</td><td>";
		echo "<a href=\"view_employee.php?email=$email\">$firstName $lastName</a>";
		echo "</td><td>";
		echo $row['Email'];
		echo "</td></tr>";
	}
	echo "</table>";

	echo "<input type=\"hidden\" name=\"projectID\" value=\"$pid\">";
	echo "<p><input type=\"submit\" name=\"save\" value=\"Save\" />"; 
	echo "</form>"; 
  
  echo "<a href=\"dashboard.php\">Dashboard</a>";
  
  if(!strcmp($pid, $defaultIDString))
  {
    echo "<p>Project Milestones";
  }
  else
  {
    echo "<p><a href=\"project_milestones.php?projectID=$pid\">Project Milestones</a>";
  }
  
	echo "</body>"; 
	echo "</html>"; 

}
else
{
	echo "Must be logged in to view this page.";
}

?>
